import com.song.service.MaraTempService;
import com.song.service.TaskService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.*;

//确保你的类是一个Spring管理的Bean。通过将类标记为@Component


public class Test01 {



    public static void main(String[] args) {
        String url = "jdbc:mysql://192.168.37.117:8899/tn_mdm_test?allowMultiQueries=true&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8";
        String username = "mdm_test";
        String password = "test007";

        try (Connection conn = DriverManager.getConnection(url, username, password)) {
            String sql = "SELECT * FROM `tn_mdm_test`.`tn_work_task`";
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while (rs.next()) {
                // 获取并打印每一列的值
                System.out.println("================");
                System.out.println("Column 1: " + rs.getString(1));
                System.out.println("Column 2: " + rs.getString(2));
                System.out.println("Column 3: " + rs.getString(3));
                System.out.println("Column 4: " + rs.getString(4));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 查看Bean是否存在
        try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml")) {
            boolean isExist = context.containsBean("Test01");
            boolean isExist1 = context.containsBean("TaskService");
            boolean isExist2 = context.containsBean("MaraTempService");
            System.out.println("Bean test01 is exist: " + isExist);
            System.out.println("Bean TaskService is exist: " + isExist1);
            System.out.println("Bean MaraTempService is exist: " + isExist2);
        }


        System.out.println("===");
        // 通过代理访问，防止声明式事务失效
        //((MaraTempService) AopContext.currentProxy()).maraTempInsert();
        System.out.println("AopContext.currentProxy()===");
        //main 方法是一个新的线程，它不同于 AOP 调用上下文所在的线程。因此，无法通过 AopContext.currentProxy() 方法来获取 AOP 代理对象。
        //为了解决这个问题，可以使用 Spring 的容器来获取代理对象

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MaraTempService proxy = context.getBean(MaraTempService.class);
        proxy.maraTempInsert();
        System.out.println("ApplicationContext===");


        MaraTempService maraTempService = new MaraTempService();
        maraTempService.maraTempInsert();

        TaskService taskService = new TaskService();
        taskService.tranFather();
        taskService.tranFather1();

    }
}
